package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.ResponseInfo;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-16
 * Time: 下午3:47
 * To change this template use File | Settings | File Templates.
 */
public class KeywordAnalysisAdvanced {

    private static Logger logger = LoggerFactory.getLogger(KeywordAnalysisAdvanced.class);

    public static void main(String[] args) {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/user_keyword_20140115.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath, "GBK");
        long startTime = System.currentTimeMillis();
        String analyzeURL = "http://192.168.1.37:8098/analyze/?q=";
        String doctorURL = "http://192.168.1.37:9082/doctor_search?secondsort=0&rows=0&q=";
        String hospitalURL = "http://192.168.1.37:9082/hospital_search?rows=0&q=";
        MyHttpRequest httpRequest = new MyHttpClientRequest();
        System.out.println("request start...");
        StringBuilder sb = new StringBuilder();
        String tabString = "\t";
        for(int i = 0; i < keywordList.size(); i++){
            if(i % 1000 == 0){
                System.out.println("Deal with " + i + " keywords already.");
            }
            try {
                ResponseInfo doctorInfo = httpRequest.requestAndGetResponseInfo(doctorURL + URLEncoder.encode(keywordList.get(i), "UTF-8"));
                ResponseInfo hospitalInfo = httpRequest.requestAndGetResponseInfo(hospitalURL + URLEncoder.encode(keywordList.get(i), "UTF-8"));
                List<AnalyzeObject> analyzeObjects = JSONStreamParserForAnalyze.parse(analyzeURL + URLEncoder.encode(keywordList.get(i), "UTF-8"));
                sb.append(keywordList.get(i)).append(tabString);
                sb.append(doctorInfo.getReturnCount()).append(tabString);
                sb.append(hospitalInfo.getReturnCount()).append(tabString);
                if(analyzeObjects.size() == 1){
                    String uuid = analyzeObjects.get(0).getUuid();
                    if(uuid != null && !uuid.trim().isEmpty()){
                        sb.append(analyzeObjects.get(0).getType()).append(tabString);
                    }
                }
                sb.append("\n");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }
        logger.info(sb.toString());
        System.out.println("request end ...");
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }

}
